package com.eclink.iot.rfid;

import com.eclink.iot.base.DeviceBase;
import com.eclink.iot.base.SocketClientBase;
import com.eclink.utils.ByteUtils;
import java.net.Socket;


public class RFIDDeviceClient
  extends DeviceBase
{
  protected static final int API_COMMAND_INVENTORY = 34;
  protected static final int API_COMMAND_INVENTORY_MULTI = 39;
  protected static final int API_COMMAND_STOP = 40;
  private int api_inventory_errorcode = 255;
  
  private int api_inventory_multi_errorcode = 255;
  
  private int api_stop_errorcode = 255;
  
  public RFIDTagDataListener rfid_tag_data_Listener;
  
  public RFIDDeviceClient(Socket socket, SocketClientBase.ConnectStatusChangeListener listener) {
    super(socket, listener);
    
    this.TAG = "RFIDDeviceClient";
    
    stop_inventory();
  }
  
  public void set_rfid_tag_data_listener(RFIDTagDataListener listener) {
    this.rfid_tag_data_Listener = listener;
  }
  
  public boolean inventory(int ant) {
    try {
      this.api_inventory_errorcode = 255;
      api_build_command(34, new byte[] { (byte)(ant >> 24 & 0xFF), (byte)(ant >> 16 & 0xFF), (byte)(ant >> 8 & 0xFF), (byte)(ant & 0xFF) });
      
      long time = System.currentTimeMillis();
      while (System.currentTimeMillis() - time < 5000L && this.api_inventory_errorcode == 255) {
        Thread.sleep(10L);
      }
      
      return (this.api_inventory_errorcode == 0);
    } catch (Exception ex) {
      return false;
    } 
  }
  
  public boolean inventory_multi(int ant) {
    try {
      this.api_inventory_multi_errorcode = 255;
      api_build_command(39, new byte[] { (byte)(ant >> 24 & 0xFF), (byte)(ant >> 16 & 0xFF), (byte)(ant >> 8 & 0xFF), (byte)(ant & 0xFF) });
      
      long time = System.currentTimeMillis();
      while (System.currentTimeMillis() - time < 5000L && this.api_inventory_multi_errorcode == 255) {
        Thread.sleep(10L);
      }
      
      return (this.api_inventory_multi_errorcode == 0);
    } catch (Exception ex) {
      return false;
    } 
  }
  
  public boolean stop_inventory() {
    try {
      this.api_stop_errorcode = 255;
      api_build_command(40, new byte[0]);
      
      long time = System.currentTimeMillis();
      while (System.currentTimeMillis() - time < 5000L && this.api_stop_errorcode == 255) {
        Thread.sleep(10L);
      }
      
      return (this.api_stop_errorcode == 0);
    } catch (Exception ex) {
      return false;
    } 
  }
  
  protected void command_receiving_notification(int type, byte[] buffer) {
    switch (type) {
      case 34:
        this.api_inventory_errorcode = 0;
        if (this.rfid_tag_data_Listener != null) {
          int epc_length = (buffer[8] << 8 & 0xFF00 | buffer[7] & 0xFF) / 8 * 2;
          int ant = buffer[5];
          String epc =  ByteUtils.toHexString(buffer, 9, epc_length, "");
          this.rfid_tag_data_Listener.notice(this.uuid, ant, epc);
        } 
        break;
      
      case 39:
        this.api_inventory_multi_errorcode = buffer[5];
        break;
      
      case 40:
        this.api_stop_errorcode = buffer[5];
        break;
    } 
  }
}


/* Location:              E:\code\work\eclink\eclink-iot-tcp-server\backend\libs\eclink-iot.jar!\com\eclink\iot\rfid\RFIDDeviceClient.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */